package org.itboys.mobile.service.mongo.web;


import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.entity.mongo.TianJieFinanceGame;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：天街_金融_游戏_service
 */
@Service
public class TianJieGameService extends BaseMongoService<TianJieFinanceGame> {


    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Autowired
    private SundayUploadService uploadService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<TianJieFinanceGame> getEntityClass() {
        return TianJieFinanceGame.class;
    }

    /**
     * 分页查询
     * @param request
     * @return
     */
    public PageResult<TianJieFinanceGame> selectGame(HttpServletRequest request){
        Map<String,Object> param =new HashMap<String,Object>();
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"name");
        return super.containpageQuery(request,containsparam,param);
    }

    /**
     * 新增或保存
     * @param Game
     */
    public void saveGame(TianJieFinanceGame Game,MultipartFile imageFile) throws Exception{
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
            Game.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(Game.getId())){
            super.updateExceptEmpty(Game.getId(),Game);
        }else{
            super.save(Game);
        }
    }


    /**
     * 单个查询
     * @param id
     * @return
     */
    public TianJieFinanceGame findOne(Long id){
        TianJieFinanceGame Game=null;
        if(LongUtil.isNotZreo(id)){
            Game=super.getById(id);
        }else{
            Game=new TianJieFinanceGame();
            Game.setId(0l);
        }
        return Game;
    }






}
